﻿using System;
using Redmine.Laptop.API;
using Redmine.Laptop.Common.Constants;
using Redmine.Laptop.Common.Settings.SettingsUtilities;
using Redmine.Laptop.Reports.ReportParameters;
using Redmine.Laptop.Reports.Reports;

namespace Redmine.Laptop.Reports.Factories.ReportFactories
{
    internal class ReportFactory : IReportFactory
    {
        private readonly RedmineService _redmineService;

        private static ReportFactory _instance;
        public static ReportFactory Instance { get { return _instance ?? (_instance = new ReportFactory()); } }
        
        private ReportFactory()
        {
            var host = CommonSettings.RedmineSettings.Host;
            var isUsedApiAuth = CommonSettings.RedmineSettings.IsUsedApiAuthentication;
            
            _redmineService = isUsedApiAuth 
                ? new RedmineService(host, CommonSettings.RedmineSettings.ApiKey) 
                : new RedmineService(host, CommonSettings.RedmineSettings.Login, CommonSettings.RedmineSettings.Password);
        }

        public Report Create(ReportType type, Parameters parameters)
        {
            switch (type)
            {
                case ReportType.Timesheet:
                    return new TimesheetReport(_redmineService, type, parameters);
                case ReportType.EmploymentRates:
                    return new EmploymentRatesReport(_redmineService, type, parameters);
                default: 
                    throw new ArgumentException(Constant.ExceptionMessage.UnknownReportType);
            }
        }
    }
}
